cmake_minimum_required(VERSION 2.4.0)

SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)

IF(COMMAND cmake_policy)
	cmake_policy(SET CMP0003 NEW)
	cmake_policy(SET CMP0005 OLD)
	cmake_policy(SET CMP0007 NEW)
	cmake_policy(SET CMP0015 NEW)
	cmake_policy(SET CMP0020 NEW)
ENDIF()

IF(LINK_STATIC_RUNTIME AND PROJECT_NAME_DEPENDS_ON_LINKAGE)
	PROJECT(UFTT-static)
ELSE()
	PROJECT(UFTT)
ENDIF()

# Treat warnings as errors cmake option
SET(TREAT_WARNINGS_AS_ERRORS ON CACHE BOOL "Treat warnings as errors")

IF(MSVC)
	SET(LINK_STATIC_RUNTIME OFF CACHE BOOL "Link statically against C++ runtime")
	SET(PROJECT_NAME_DEPENDS_ON_LINKAGE OFF CACHE BOOL "Change project name when statically linking (generates a solution with a different filename)")

	IF(TREAT_WARNINGS_AS_ERRORS)
		SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX")
		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
	ENDIF()

	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /bigobj")

	# Extra speed optimisation options
	SET(MSVC_EXTRA_OPTIMIZE ON CACHE BOOL "Use extra optimization flags in release builds")
	IF(MSVC_EXTRA_OPTIMIZE)
		SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Oi /Ot /Oy /GL /GS- /fp:fast")
		SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Oi /Ot /Oy /GL /GS- /fp:fast")
		SET(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
		SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /OPT:REF /OPT:ICF /LTCG")
		SET(CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS_RELEASE} /OPT:REF /OPT:ICF /LTCG")
	ENDIF()

	# Even more speed optimisations
	SET(MSVC_OPTIMIZE_SSE2 OFF CACHE BOOL "Use SSE2 instruction set in release builds")
	IF(MSVC_OPTIMIZE_SSE2)
		SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /arch:SSE2")
		SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:SSE2")
	ENDIF()

	#SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:LIBCMTD")
	#SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /NODEFAULTLIB:LIBCMT /NODEFAULTLIB:libcpmt")
	#SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:msvcrt")

	IF(LINK_STATIC_RUNTIME)
		foreach(cfgtype ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})
			string(TOUPPER "CMAKE_C_FLAGS_${cfgtype}" flag_var)
			string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
			string(TOUPPER "CMAKE_CXX_FLAGS_${cfgtype}" flag_var)
			string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
		endforeach()
		ADD_DEFINITIONS("-DLINK_STATIC_RUNTIME")
		#SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:msvcrt /NODEFAULTLIB:msvcrtd")
	ENDIF()
ENDIF()

IF(UNIX AND NOT MACOSX AND NOT CYGWIN)
	SET(LINUX 1)
ELSE()
	SET(LINUX 0)
ENDIF()

IF(ANDROID)
	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions -frtti")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fexceptions")
ENDIF()

IF(UNIX)
	ADD_DEFINITIONS("-D_LARGEFILE_SOURCE")
	ADD_DEFINITIONS("-D_FILE_OFFSET_BITS=64")
ENDIF()

IF(WIN32)
	ADD_DEFINITIONS("-DNOMINMAX")
	ADD_DEFINITIONS("-D_SCL_SECURE_NO_WARNINGS")
	ADD_DEFINITIONS("-D_CRT_SECURE_NO_WARNINGS")
ENDIF()

IF(CMAKE_COMPILER_IS_GNUCXX)
	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -pedantic")
	IF(TREAT_WARNINGS_AS_ERRORS)
		SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
	ENDIF()
ENDIF()

IF(MINGW)
	SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -mwindows")
ENDIF()

IF(WIN32)
	SET(WINDOWS_9X_SUPPORT OFF CACHE BOOL "Build executable compatible with windows 9x")
	IF(WINDOWS_9X_SUPPORT)
		ADD_DEFINITIONS("-D_WIN32_WINDOWS")
	ELSE()
		ADD_DEFINITIONS("-D_WIN32_WINNT=0x0501")
	ENDIF()
ENDIF()

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/")

SUBDIRS(src)

Set(TESTING_ENABLE OFF CACHE BOOL "Enable uftt automatic testing.")
If(TESTING_ENABLE)
	ENABLE_TESTING()
	SUBDIRS(tests)
Endif()
